﻿using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour
{
	public float speed;
	public bool grounded, interact;
	public Transform jumpCheck, interactCheck; 
	Animator anim;

	RaycastHit2D interacted; 

	void Start()
	{
		anim = GetComponent<Animator> ();
		Physics2D.IgnoreLayerCollision(8, 10);
	}

	void Update()
	{
		Movement();
		RaycastStuff(); 
	}

	void RaycastStuff()
	{

		Debug.DrawLine(transform.position, jumpCheck.position, Color.magenta);
		Debug.DrawLine(transform.position, interactCheck.position, Color.magenta);


		grounded = Physics2D.Linecast(transform.position, jumpCheck.position, 1 << LayerMask.NameToLayer("Ground"));  


		if(Physics2D.Linecast(transform.position, interactCheck.position, 1 << LayerMask.NameToLayer("Guard")))
		{

			interacted = Physics2D.Linecast(transform.position, interactCheck.position, 1 << LayerMask.NameToLayer("Guard")); 
			interact = true; 
		}
		else
		{
			interact = false; 
		}

		Physics2D.IgnoreLayerCollision(8, 10);
	}


	void Movement() 
	{
		anim.SetFloat ("speed",Mathf.Abs(Input.GetAxis("Horizontal")));

		if(Input.GetAxisRaw("Horizontal") > 0)
		{
			transform.Translate(Vector3.right  *speed * Time.deltaTime); 
			transform.eulerAngles = new Vector2(0, 0); 
		}
		else if(Input.GetAxisRaw("Horizontal") < 0)
		{
			transform.Translate(Vector3.right  *speed* Time.deltaTime);
			transform.eulerAngles = new Vector2(0, 180); 
		}

		if(Input.GetKeyDown (KeyCode.Space) && grounded)
		{

			rigidbody2D.AddForce(transform.up * 200f);
		}
	}
}
